<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../statics/layui/css/layui.css">
    <style>

    </style>
</head>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script type="text/javascript" src="../statics/layui/layui.all.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','jquery'], function(table, $){
        var $ = layui.jquery, table = layui.table;

        table.render({
            elem: '#test'
            ,url:'userlist.action'
            ,toolbar: '#toolbarDemo'

            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'name', title:'用户名', width:120, edit: 'text'}
                ,{field:'telephone', title:'电话号', width:80, edit: 'text', sort: true}
                ,{field:'username', title:'用户名', width:100}
                ,{field:'isadmin', title:'是否管理员',width:100}
                ,{field:'createTime', title:'创建时间',sort: true}
            ]]
            ,page: true //开启分页
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                var arr = $.parseJSON(res);
                let v = {};

                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "getUserCount.action",
                    data: {
                    },
                    async: false,
                    error: function (request) {
                       /* $.modal.alertError("系统错误");*/
                    },
                    success: function (data) {
                        v.total = data;
                    }
                });
                if(res && res.length >=0){
                    v.status = 0
                    v.message = '成功'
                    v.list= arr
                }else{
                    v.total = 0
                    v.status = 1
                    v.message = '失败'
                    v.list= []
                }
                return {
                    "code": v.status, //解析接口状态
                    "msg": v.message, //解析提示文本
                    "count": v.total, //解析数据长度
                    "data": v.list //解析数据列表
                };
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>
</html>